package org.javaboy.vhr.controller.system.basic;

import org.javaboy.vhr.bean.Position;
import org.javaboy.vhr.bean.RespBean;
import org.javaboy.vhr.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {

    @Autowired
    PositionService positionService;


    //查询所有职位，用get请求
    @GetMapping("/")
    public List<Position> getAllPositions(){
        return positionService.getAllPositions();
    }

    //添加职位，用post请求
    @PostMapping("/")
    public RespBean addPosition(@RequestBody Position position){ //因为返回的是json，所以要加个@RequestBody
        if (positionService.addPosition(position) == 1){
            return RespBean.ok("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    //修改职位
    @PutMapping("/")
    public RespBean updatePositions(@RequestBody Position position){
        if (positionService.updatePositions(position) == 1){
            return RespBean.ok("修改成功!");
        }
        return RespBean.error("修改失败！");
    }

    //删除职位
    @DeleteMapping("/{id}")
    public RespBean deleteRositionById(@PathVariable Integer id){
        if (positionService.deleteRositionById(id) == 1){
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    //批量删除职位
    // 因为是删除多个，所以id可以是以数组的形式传过来，也可以是以字符串传过来，这里使用数组的方式
    @DeleteMapping("/")
    public RespBean deletePositionsByIds(Integer[] ids){
        if (positionService.deletePositionsByIds(ids) == ids.length) {
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }


}
